// Author: Zhang Yisong(zhang_yisong@foxmail.com)

#pragma once

#include "pw_allocator/first_fit_block_allocator.h"
#include "pw_preprocessor/arguments.h"
#include "pw_preprocessor/compiler.h"
#include <array>

namespace coro {

template <size_t kByteNum>
using ContextBuffer = std::array<std::byte, kByteNum>;

using Allocator = pw::allocator::FirstFitBlockAllocator<uintptr_t>;

#if CONFIG_CORO_GLOBAL_BUFF
inline static ContextBuffer<CONFIG_CORO_GLOBAL_BUFF_SIZE> buffer;
inline auto &GetGlobalAllocator() {
  static Allocator alloc{buffer};
  return alloc;
}
#endif

#define CORO_CTX_BUF_DEFINE(...)                                               \
  PW_DELEGATE_BY_ARG_COUNT(_CORO_CTX_BUF_DEFINE, __VA_ARGS__)
#define _CORO_CTX_BUF_DEFINE2(name, size)                                      \
  inline static coro::ContextBuffer<size> name{};
#define _CORO_CTX_BUF_DEFINE3(name, size, section)                             \
  inline static PW_KEEP_IN_SECTION(section) coro::ContextBuffer<size> name{};

} // namespace coro